package com.webserver.http;

import java.util.HashMap;
import java.util.Map;

/**
 * 该类定义所有和HTTP协议有关可被复用的数据信息
 */
public class HttpContext {
    /**
     * 回车符
     */
    public static final char CR = 13;
    /**
     * 换行符
     */
    public static final char LF = 10;
    /**
     * 存放所有资源的后缀与Content-Type头的对应关系
     * key:资源后缀名
     * value:Content-Type头对应的值
     */
    private static Map<String, String> mimeMapping = new HashMap<>();
    static {
        //初始化mimeMapping
        initMimeMapping();
    }

    private static void initMimeMapping() {
        mimeMapping.put("html", "text/html");
        mimeMapping.put("css", "text/css");
        mimeMapping.put("js", "application/javascript");
        mimeMapping.put("png", "image/png");
        mimeMapping.put("gif", "image/gif");
        mimeMapping.put("jpg", "image/jpeg");
    }

    /**
     * 根据资源后缀名获取对应的Context-Type
     * @param ext
     * @return
     */
    public static String getMimeType(String ext){
        return mimeMapping.get(ext);
    }
}
